// 正则表达式匹配
#include <iostream>

bool MatchCore(char *str, char *pattern)
{
    if (*str == '\0' && *pattern == '\0')
        return true;

    // 模式匹配到头了，str字符串字符数多了。
    if (*str != '\0' && *pattern == '\0')
        return false;
    // str字符串字符数少了，模式没有全部匹配
    if (*str == '\0' && *pattern != '\0')
        return false;

    if (*(pattern + 1) == '*')
    {
        if (*str == *pattern || *pattern == '.')
        {
            return MatchCore(str, pattern + 2) || MatchCore(str + 1, pattern + 2) || MatchCore(str + 1, pattern);
        }
        else
        {
            return MatchCore(str, pattern + 2);
        }
    }
    if (*str == *pattern || *pattern == '.')
    {
        return MatchCore(str + 1, pattern + 1);
    }
    // 没有匹配上
    return false;
}

bool Match(char *str, char *pattern)
{
    if (str == nullptr || pattern == nullptr)
        return false;

    return MatchCore(str, pattern);
}

int main()
{
    // ‘.’验证
    char *str = "abcdefg";
    char *pattern = "ab.d.f.";
    std::cout << Match(str, pattern) << std::endl;

    // ‘*’匹配0次验证
    pattern = "abch*defg";
    std::cout << Match(str, pattern) << std::endl;
    //‘*’匹配0次验证
    pattern = "abcc*defg";
    std::cout << Match(str, pattern) << std::endl;
    // ‘*’匹配1次验证
    pattern = "abc*defg";
    std::cout << Match(str, pattern) << std::endl;
    //‘*’匹配2次验证
    str = "abccdefg";
    pattern = "abc*defg";
    std::cout << Match(str, pattern) << std::endl;

    str = "";
    pattern = "abc*defg";
    std::cout << Match(str, pattern) << std::endl;

    str = "abcdefg";
    pattern = "";
    std::cout << Match(str, pattern) << std::endl;

    str = "abcdefg";
    pattern = "ab.*de*f*h*j*k*g*.*";
    std::cout << Match(str, pattern) << std::endl;

    str = "abcdefg";
    pattern = "ab.*de*f*h*daj*k*g*.*";
    std::cout << Match(str, pattern) << std::endl;

    str = "aaaa";
    pattern = "a*aaa";
    std::cout << Match(str, pattern) << std::endl;

    return 0;
}